<template>
  <view class="detail-container">
    <!-- 教师基本信息卡片 -->
    <view class="teacher-card">
      <image class="teacher-avatar" :src="teacherInfo.cover_image" mode="aspectFill"></image>
      <view class="teacher-info">
        <view class="teacher-header">
          <text class="teacher-name">{{ teacherInfo.name }}</text>
          <view class="certification-tag" v-if="teacherInfo.certificationStatus === 2">已认证</view>
        </view>
        <view class="teacher-subjects">擅长：{{ teacherInfo.subjectNames }}</view>
        <view class="teacher-exp">教龄：{{ teacherInfo.teachingAge }}年</view>
      </view>
    </view>

    <!-- 教师介绍 -->
    <view class="section">
      <view class="section-title">教师介绍</view>
      <view class="section-content">{{ teacherInfo.introduction }}</view>
    </view>

    <!-- 课程信息 -->
    <view class="section">
      <view class="section-title">课程信息</view>
      <view class="course-list">
        <view class="course-item" v-for="course in teacherCourses" :key="course.id">
          <image class="course-image" :src="course.cover_image" mode="aspectFill"></image>
          <view class="course-info">
            <text class="course-title">{{ course.title }}</text>
            <view class="course-price">
              <text class="price">¥{{ course.price }}</text>
              <text class="original-price" v-if="course.original_price">¥{{ course.original_price }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部操作栏 -->
    <view class="bottom-bar">
      <view class="action-buttons">
        <button class="contact-btn" @tap="handleContact">联系老师</button>
        <button class="signup-btn" @tap="handleSignup">立即报名</button>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/utils/request.js'
import { recommendApi } from '@/api/recommend.js'

export default {
  data() {
    return {
      teacherId: null,
      teacherInfo: {
        name: '',
        cover_image: '',
        subjectNames: '',
        teachingAge: 0,
        introduction: '',
        certificationStatus: 1
      },
      teacherCourses: []
    }
  },
  
  onLoad(options) {
    if (options.id) {
      this.teacherId = options.id
      this.getTeacherDetail()
      
      // 记录浏览行为
      this.trackBehavior('view')
    }
  },
  
  methods: {
    async getTeacherDetail() {
      try {
        const res = await request({
          url: `http://203.34.152.33/user/teacher/detail/${this.teacherId}`,
          method: 'GET'
        })
        
        if (res.code === 200) {
          this.teacherInfo = {
            ...res.data,
            cover_image: res.data.cover_image || '/static/default-teacher.png'
          }
          // 获取该教师的课程列表
          this.getTeacherCourses()
        }
      } catch (error) {
        console.error('获取教师详情失败:', error)
        uni.showToast({
          title: '获取教师详情失败',
          icon: 'none'
        })
      }
    },
    
    async getTeacherCourses() {
      try {
        const res = await request({
          url: `http://203.34.152.33/user/api/courses/teacher/${this.teacherId}`,
          method: 'GET'
        })
        
        if (res.code === 200) {
          this.teacherCourses = res.data
        }
      } catch (error) {
        console.error('获取教师课程失败:', error)
      }
    },
    
    handleContact() {
      // 实现联系教师功能
      uni.showToast({
        title: '正在连接客服...',
        icon: 'none'
      })
    },
    
    handleSignup() {
      // 实现报名功能
      this.trackBehavior('enroll')
      uni.navigateTo({
        url: `/pages/signup/signup?teacherId=${this.teacherId}`
      })
    },
    
    // 记录用户行为
    trackBehavior(action) {
      const userInfo = uni.getStorageSync('userInfo')
      if (userInfo && userInfo.id && this.teacherId) {
        recommendApi.trackUserBehavior({
          userId: userInfo.id,
          courseId: this.teacherId,
          action: action,
          timestamp: new Date().getTime()
        }).catch(err => {
          console.error('行为跟踪记录失败:', err)
        })
      }
    }
  }
}
</script>

<style lang="scss">
.detail-container {
  padding: 20rpx;
  padding-bottom: 120rpx; /* 为底部操作栏留出空间 */
  background-color: #f5f5f5;
}

.teacher-card {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  display: flex;
  margin-bottom: 20rpx;
}

.teacher-avatar {
  width: 160rpx;
  height: 160rpx;
  border-radius: 80rpx;
  margin-right: 30rpx;
}

.teacher-info {
  flex: 1;
}

.teacher-header {
  display: flex;
  align-items: center;
  margin-bottom: 16rpx;
}

.teacher-name {
  font-size: 36rpx;
  font-weight: bold;
  margin-right: 20rpx;
}

.certification-tag {
  background: #4080FF;
  color: #fff;
  font-size: 24rpx;
  padding: 4rpx 16rpx;
  border-radius: 20rpx;
}

.teacher-subjects {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 12rpx;
}

.teacher-exp {
  font-size: 28rpx;
  color: #666;
}

.section {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
  color: #333;
}

.section-content {
  font-size: 28rpx;
  color: #666;
  line-height: 1.6;
}

.course-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.course-item {
  background: #fff;
  border-radius: 8rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.course-image {
  width: 100%;
  height: 180rpx;
  background: #f5f5f5;
}

.course-info {
  padding: 16rpx;
}

.course-title {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 8rpx;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.course-price {
  display: flex;
  align-items: baseline;
}

.price {
  font-size: 32rpx;
  color: #ff6b6b;
  font-weight: bold;
}

.original-price {
  font-size: 24rpx;
  color: #999;
  text-decoration: line-through;
  margin-left: 12rpx;
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  padding: 20rpx;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.action-buttons {
  display: flex;
  gap: 20rpx;
}

.contact-btn, .signup-btn {
  flex: 1;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  border-radius: 40rpx;
  font-size: 28rpx;
}

.contact-btn {
  background: #fff;
  color: #4080FF;
  border: 2rpx solid #4080FF;
}

.signup-btn {
  background: #4080FF;
  color: #fff;
}
</style> 